package com.winz.filterbar;

import android.content.Context;
import android.database.Observable;
import androidx.annotation.Nullable;
import android.util.AttributeSet;
import android.view.View;
import android.widget.LinearLayout;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by terry
 */

public class FilterBar extends LinearLayout implements FilterTab.OnTabSelectedChangeListener, View.OnClickListener {

    private TabSelectedObervable tabSelectedObervable = new TabSelectedObervable();
    private SelectedMenuListener selectedMenuListener;

    private List<FilterTab> tabs = new ArrayList<>();

    public FilterBar(Context context) {
        this(context, null);
    }

    public FilterBar(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public FilterBar(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        setOrientation(HORIZONTAL);

    }


    public interface OnTabSelectedChangeListener{
        void onChange(FilterTab filterTab, boolean selected, int flag);
    }

    private  class TabSelectedObervable extends Observable<OnTabSelectedChangeListener>{

        public void notifyTabChanged(FilterTab filterTab,boolean selected,int flag){
            synchronized(mObservers) {
                for (int i = mObservers.size() - 1; i >= 0; i--) {
                    mObservers.get(i).onChange(filterTab,selected,flag);
                }
            }
        }
    }


    @Override
    protected void onFinishInflate() {
        super.onFinishInflate();
        syncFilterTabState();
    }

    private void syncFilterTabState() {

        for (int i = 0; i < getChildCount(); i++) {
            View child = getChildAt(i);
            if (child instanceof FilterTab) {
                ((FilterTab) child).addTabSelectedChangeListener(FilterBar.this);
                child.setOnClickListener(this);
                tabs.add((FilterTab) child);
            }
        }
    }


    private FilterTab lastSelectedTab;

    @Override
    public void onChange(FilterTab filterTab, boolean selected,int flag) {

        if (selected) {
            if (lastSelectedTab != null) {

                lastSelectedTab.setFilterTabSelected(false,FilterTab.FLAG_OTHER);
            }
            lastSelectedTab = filterTab;
        }else {
            lastSelectedTab = null;
        }
        tabSelectedObervable.notifyTabChanged(filterTab,selected,flag);
        if(selectedMenuListener != null){
            selectedMenuListener.onSelectedMenu(filterTab,filterTab.isSelected());
        }
    }
    /**
     * 添加状态改变的监听
     * @param listener
     */
    public void addTabSelectedChangeListener(OnTabSelectedChangeListener listener){
        tabSelectedObervable.registerObserver(listener);
    }

    public List<FilterTab> getTabs(){
        return tabs;
    }


    @Override
    public void onClick(View view) {
        if(view instanceof FilterTab){
            ((FilterTab) view).setFilterTabSelected(!view.isSelected(),FilterTab.FLAG_CLICK);

        }

    }

    public void setSelectedMenuListener(SelectedMenuListener selectedMenuListener){
        this.selectedMenuListener = selectedMenuListener;
    }

    public interface SelectedMenuListener {
        void onSelectedMenu(FilterTab view, boolean selected);
    }

    public TabSelectedObervable getTabSelectedObervable(){
        return tabSelectedObervable;
    }
}
